package backend;

import backend.Cpu.Interrupt;
import backend.Cpu.Interrupt_Handle;

/**
 * SchedulerTimer.java
 * 
 * Simulates the Timer for the CPU.
 * 
 * @author BitWiseGuys Dallas Rosson, Joshua Warner, Tyson Lewis, Ken Norton
 * @version 1.0, 5/29/2012
 */

public class SchedulerTimer{
	private Cpu my_cpu;
	final int CYCLE_TIME = 5000;
	
	public SchedulerTimer(Cpu the_cpu) {
		
		my_cpu = the_cpu;
		
	}
	
	public void start(){
		Timer timer = new Timer();
		Thread t = new Thread(timer);
		t.setDaemon(true);
		t.start();
	}
	
	
	/**
	 * Runnable subclass for to simulate the time it would 
	 * take to execute a request and then start the next request.
	 */
	private class Timer implements Runnable{
		
		@Override
		public void run() {
			while(true){
				try{
					Thread.sleep(CYCLE_TIME);
				}catch(InterruptedException e){
				//do nothing sit-n-spin
				}

				while(my_cpu.getInterruptState().equals(Cpu.Interrupt.DISABLED)){
					//sit-n-spin
				}
				my_cpu.setInterruptHandle(Interrupt_Handle.TIMER_INT_HANDLE);
			
				//System.out.println("Timer says cycle time over");
			}
		}
	}
}
